//
//  XNFixPayViewController.m
//  XINNingOA
//
//  Created by samuel on 2022/3/29.
//

#import "XNFixPayViewController.h"
#import "XNFixPayApproveRequest.h"
#import "XNFixPayBillInfoRequest.h"

@interface XNFixPayViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNFixPayBillInfoModel *infoModel;

@end

@implementation XNFixPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNFixPayBillInfoRequest *infoRequest = [[XNFixPayBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNFixPayBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNFixPayBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNFixPayApproveRequest *approveRequest = [[XNFixPayApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNFixPayApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNFixPayApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}


- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                              [NSString stringWithFormat:@"申请人： %@",self.infoModel.applicantName],
                              [NSString stringWithFormat:@"来源单号： %@",self.infoModel.fixedApplyId.length > 0 ? self.infoModel.fixedApplyId : @""],
                              [NSString stringWithFormat:@"供应商： %@",self.infoModel.supplierName],
                              [NSString stringWithFormat:@"预算目录： %@",self.infoModel.budgetName.length > 0 ? self.infoModel.budgetName : @""],
                              [NSString stringWithFormat:@"发票类型： %@",self.infoModel.invoiceTypeName.length > 0 ? self.infoModel.invoiceTypeName : @""],
                              [NSString stringWithFormat:@"税率： %@",self.infoModel.taxCode],
                              [NSString stringWithFormat:@"单据张数： %ld",(long)self.infoModel.appendixNum],
                              [NSString stringWithFormat:@"报销金额(含税)： %.3f",self.infoModel.amountWithDuty],
                              [NSString stringWithFormat:@"报销金额(不含税)：： %.3f",self.infoModel.amountWithOutDuty],
                              [NSString stringWithFormat:@"备注： %@",self.infoModel.remark.length > 0 ? self.infoModel.remark:@""],
                              @"附件信息",
                              nil];
    itemModel.showItemArray = showItemArray;
    itemModel.fileArray = self.infoModel.fileList;
    itemModel.fileIsImage = NO;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
}

@end
